package com.kk.prototype;

import java.io.IOException;

/**
 * 原型模式
 *
 * @author 王志强
 * @date 2021-12-14
 */
public class Client {
    public static void main(String[] args) throws IOException {

        System.out.println(" 原型模式完成克隆羊");

        Sleep sleep = new Sleep();
        sleep.friend = new Sleep();
        sleep.setAge(22);
        sleep.setAddress("蒙古");
        sleep.setName("绵羊");

        System.out.println(sleep);

        /**
         * 浅拷贝，嵌套对象的引用值（内存地址）被传递
         */
        System.out.println("\n 浅拷贝：");
        Sleep clone = (Sleep) sleep.clone();
        Sleep clone1 = (Sleep) sleep.clone();
        Sleep clone2 = (Sleep) sleep.clone();

        System.out.println("clone = " + clone + "    " + clone.hashCode());
        System.out.println(clone.friend.hashCode());

        System.out.println("clone1 = " + clone1 + "    " + clone1.hashCode());
        System.out.println(clone1.friend.hashCode());

        System.out.println("clone2 = " + clone2 + "    " + clone2.hashCode());
        System.out.println(clone1.friend.hashCode());


        /**
         * 深拷贝，通过对象的序列化实现
         */
        System.out.println("\n 深拷贝：");
        Sleep deeepClone = (Sleep) sleep.deepClone();
        Sleep deeepClone1 = (Sleep) sleep.deepClone();
        Sleep deeepClone2 = (Sleep) sleep.deepClone();

        System.out.println("clone = " + deeepClone + "    " + deeepClone.hashCode());
        System.out.println(deeepClone.friend.hashCode());

        System.out.println("deeepClone1 = " + clone1 + "    " + deeepClone1.hashCode());
        System.out.println(deeepClone1.friend.hashCode());

        System.out.println("deeepClone2 = " + clone2 + "    " + deeepClone2.hashCode());
        System.out.println(deeepClone2.friend.hashCode());


    }
}
